<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        /* 保持原有样式不变 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "微软雅黑", sans-serif;
        }

        body {
            background-color: #f5f5f5;
            padding: 50px 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .register-container {
            width: 400px;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
            font-size: 24px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }

        input {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3a80d2;
        }

        .footer-text {
            text-align: center;
            margin-top: 20px;
            color: #999;
            font-size: 14px;
        }

        .footer-text a {
            color: #4a90e2;
            text-decoration: none;
        }

        .footer-text a:hover {
            text-decoration: underline;
        }

        /* 新增错误提示样式 */
        .error-message {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h1>用户注册</h1>
    <!-- 移除form的action和method，通过JS处理提交 -->
    <form id="registerForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            <div class="error-message" id="usernameError"></div>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <div class="error-message" id="passwordError"></div>
        </div>
        <button type="submit">注册</button>
        <p class="footer-text">已有账号？<a href="/login.html">去登录</a></p>
    </form>
</div>

<script>
    // 添加表单验证
    const form = document.getElementById('registerForm');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    const usernameError = document.getElementById('usernameError');
    const passwordError = document.getElementById('passwordError');

    // 输入验证函数
    function validateUsername() {
        if (usernameInput.value.length < 3) {
            usernameError.textContent = '用户名至少3个字符';
            usernameError.style.display = 'block';
            return false;
        }
        usernameError.style.display = 'none';
        return true;
    }

    function validatePassword() {
        if (passwordInput.value.length < 6) {
            passwordError.textContent = '密码至少6个字符';
            passwordError.style.display = 'block';
            return false;
        }
        passwordError.style.display = 'none';
        return true;
    }

    // 实时验证
    usernameInput.addEventListener('blur', validateUsername);
    passwordInput.addEventListener('blur', validatePassword);

    form.addEventListener('submit', async (e) => {
        e.preventDefault();

        // 表单验证
        const isUsernameValid = validateUsername();
        const isPasswordValid = validatePassword();

        if (!isUsernameValid || !isPasswordValid) {
            return;
        }

        // 收集表单数据
        const formData = {
            username: usernameInput.value,
            password: passwordInput.value,
        };

        try {
            const response = await fetch('/auth/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(formData)
            });

            if (response.ok) {
                const result = await response.json();
                alert('注册成功！即将跳转到登录页');
                window.location.href = '/login.html';
            } else {
                const error = await response.json().catch(() => ({message: '注册失败'}));
                alert(`注册失败：${error.message}`);
            }
        } catch (error) {
            console.error('请求失败：', error);
            alert('网络错误，请稍后重试');
        }
    });
</script>
</body>
</html>